Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
El código devuelve [4, 5, 6, 7, 8] en lugar de [4, 5, 7]. El ciclo no reconoce que [6] y [8] están en estatuas variables

 function solution(statues) { statues.sort() let fullArray = []; let missingStatues = []; for (let i = Math.min(...statues); i <= Math.max(...statues); i++) { fullArray.push(i); } for (let i = 0; i < fullArray.length; i++) { if (!(fullArray[i] in statues)) { missingStatues.push(fullArray[i]); } } let missingStatuesCount = missingStatues.length; return missingStatues; } console.log(solution([6, 2, 3, 8]))

Esperaba [4, 5, 7] pero obtuve [4, 5, 6, 7, 8]. El ciclo que inserta fullArray[i] en las estatuas que faltan, también está insertando [6] y [8], que están en las estatuas y no deben insertarse.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

function solution(statues) { statues.sort()

Las comparaciones predeterminadas realizadas por el método .sort() son comparaciones de cadenas . Aparentemente, sus matrices contienen números, por lo que necesita una función de comparación numérica:

 statues.sort((a, b) => a - b);

Después:

 for (let i=Math.min(...statues);i<=Math.max(...statues);i++) { fullArray.push(i); }

Una vez que haya ordenado con éxito la matriz, statues[0] será el valor más pequeño y statues[statues.length - 1] será el más grande. No es necesario copiar la matriz y llamar a Math.min() o Math.max() .

 for (let i=0;i<fullArray.length;i++) { if (!(fullArray[i] in statues)) { missingStatues.push(fullArray[i]); } }

El operador in es para verificar claves de objetos, no valores. Lo que estás buscando es el método .includes() :

 if (!statues.includes(fullArray[i]))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!